package com.lesserpand.springcloudalibabacore.test.service.impl;

import com.lesserpand.springcloudalibabacore.config.rabbitmq.constant.MqEnum;
import com.lesserpand.springcloudalibabacore.config.rabbitmq.utils.ProviderUtils;
import com.lesserpand.springcloudalibabacore.test.dao.OrderDAO;
import com.lesserpand.springcloudalibabacore.test.entity.OrderDO;
import com.lesserpand.springcloudalibabacore.test.entity.dto.OrderDTO;
import com.lesserpand.springcloudalibabacore.test.entity.dto.OrderItemDTO;
import com.lesserpand.springcloudalibabacore.test.service.OrderItemService;
import com.lesserpand.springcloudalibabacore.test.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author bailuo
 * @version 2.0
 * @Description TODO
 * @Date Created in 2019/1/8 19:20
 */
@Service("orderService")
public class OrderServiceImpl implements OrderService {

    private static final Logger LOGGER = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Autowired
    private ProviderUtils providerUtils;

    @Resource
    private OrderDAO orderDAO;

    @Autowired
    private OrderItemService orderItemService;

    /**
     * 新增订单
     *
     * @param dto 订单信息
     */
    @Override
    @Transactional
    public String save(OrderDTO dto) {
        OrderDO orderDO = new OrderDO();
        BeanUtils.copyProperties(dto, orderDO);
        List<OrderItemDTO> list = dto.getList();
        orderDO.setOrderStatus(new Byte("1"));
        try {
            orderDAO.save(orderDO);
            list.forEach(orderItemDTO -> {
                orderItemDTO.setOrderId(orderDO.getId());
                orderItemService.save(orderItemDTO);
            });
            // 保存成功，同时放入延迟队列
            LOGGER.info("订单放入延迟队列，1.5小时后处理" + String.valueOf(orderDO.getId()));
            providerUtils.sendMessage(orderDO.getId(), MqEnum.DEAD_LETTER_EXCHANGE.getValue(), MqEnum.FIRST_MESSAGE_TTL_QUEUE_KEY.getValue(), 30000);
        } catch (Exception e) {
            LOGGER.error("订单保存失败！", e);
        }
        return "下单成功！";
    }

    /**
     * 根据订单id查询订单信息
     *
     * @param id 订单id
     */
    @Override
    public OrderDO findById(Long id) {
        OrderDO orderDO = null;
        try {
            orderDO = orderDAO.findById(id);
        } catch (Exception e) {
            LOGGER.error("查询失败！", e);
        }
        return orderDO;
    }

    /**
     * 模拟支付订单
     *
     * @param dto 订单
     * @return
     */
    @Override
    public int update(OrderDTO dto) {

        OrderDO orderDO = new OrderDO();
        BeanUtils.copyProperties(dto, orderDO);
        try {
            return orderDAO.update(orderDO);
        } catch (Exception e) {
            LOGGER.error("查询失败！", e);
        }
        return 0;
    }

    /**
     * 模拟支付订单
     *
     * @param dto 订单
     * @return
     */
    @Override
    public int payment(OrderDTO dto) {

        OrderDO orderDO = orderDAO.findById(dto.getId());
        if (!Objects.equals(orderDO.getOrderStatus(), new Byte("1"))) {
            throw new RuntimeException("订单状态不允许支付！");
        }
        orderDO.setOrderStatus(dto.getOrderStatus());
        try {
            return orderDAO.update(orderDO);
        } catch (Exception e) {
            LOGGER.error("查询失败！", e);
        }
        return 0;
    }
}
